function AthleteInfo(id, userName, profilePicture, attendantCode, lat, lng, height, speed) {
    this.id = id;
    this.username = userName;
    this.profilePicture = profilePicture;
    this.attendantCode = attendantCode;
    this.lat = lat;
    this.lng = lng;
    this.height = height;
    this.speed = speed;
    this.getLocation = function() {
        return new google.maps.LatLng(this.lat, this.lng);
    };
    this.setMarker = function(marker){
        this.marker = marker;
    };
}

function addMarker(location,map) {
    var marker = new google.maps.Marker({
        position: location,
        map: map
    });
    return marker;
}

function addMarker(location,map,image) {
    var marker = new google.maps.Marker({
        position: location,
        map: map,
        icon:image
    });
    return marker;
}

function initializeMap(listOfAthletes, map,image){
    for(i=0; i < listOfAthletes.length;i++){
        var location = listOfAthletes[i].getLocation();
        var marker = addMarker(location, map, image);
        listOfAthletes[i].setMarker(marker);
        var infowindow =[];
        google.maps.event.addListener(marker, 'click', function() {
            infowindow.push(createInfoWindow(listOfAthletes[i]));
            //            infowindow[i] = createInfoWindow(listOfAthletes[i]);
            infowindow[i].open(map, marker);
        //            alert("-->"+infowindow.length);
        });
    //        alert(infowindow.length);
    }
}

function getMarker(username){
    for(i=0; i < listOfAthletes.length;i++){
        if(username == listOfAthletes[i].username){
            return listOfAthletes[i].marker;
        }
    }
    return null;
}

function getAthleteInfo(username){
    for(i=0; i < listOfAthletes.length;i++){
        if(username == listOfAthletes[i].username){
            return listOfAthletes[i];
        }
    }
    return null;
}

//liveLocationFeedCommingEvent handler
function updateAthletePosition(jsonObject){
    var athlete = getAthleteInfo(jsonObject.username);
    athlete.lat = jsonObject.lat;
    athlete.lng = jsonObject.lng;
    athlete.height = jsonObject.height;
    athlete.speed = jsonObject.speed;
    
    if(athlete.marker != null)
        athlete.marker.setPosition(new google.maps.LatLng(athlete.lat, athlete.lng));
}

// Removes the overlays from the map, but keeps them in the array
function clearOverlays() {
    if (listOfAthletes) {
        for (athlete in listOfAthletes) {
            athlete.marker.setMap(null);
        }
    }
}

// Shows any overlays currently in the array
function showOverlays() {
    if (listOfAthletes) {
        for (athlete in listOfAthletes) {
            athlete.marker.setMap(map);
        }
    }
}

// Deletes all markers in the array by removing references to them
function deleteOverlays() {
    if (listOfAthletes) {
        for (athlete in listOfAthletes) {
            athlete.marker.setMap(map);
        }
    }
    listOfAthletes.length = 0;
}

function createInfoWindow(athleteInfo){
    var popUpContent  = "<img src='"+athleteInfo.profilePicture+"' height='30px' width='30px' />"+
    "<span style='float:right;'><b>"+athleteInfo.username+"</b><br/>"+
    "Height: "+athleteInfo.height+"<br/>"+
    "Speed: "+athleteInfo.speed+
    "<br/>Health: Good</div>";
    var infoWindow = new google.maps.InfoWindow({
        content: popUpContent
    });
    return infoWindow;
}